/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Data.java
 *
 * Created on Jun 12, 2011, 6:34:43 PM
 */
package eu.anorien.dmo.gui.optimization;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.ArrayList;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Data extends javax.swing.JPanel implements ClipboardOwner {

    static class HistoryPoint {

        private final long runTime;
        private final long iteration;
        private final double error;
        private final String parameters;

        public HistoryPoint(long runTime, long iteration, double error, String parameters) {
            this.runTime = runTime;
            this.iteration = iteration;
            this.error = error;
            this.parameters = parameters;
        }

        public long getIteration() {
            return iteration;
        }

        public double getError() {
            return error;
        }

        public String getParameters() {
            return parameters;
        }

        public long getRunTime() {
            return runTime;
        }
    }
    private ArrayList<HistoryPoint> allValues = new ArrayList<HistoryPoint>();
    private ArrayList<HistoryPoint> bestValues = new ArrayList<HistoryPoint>();

    /** Creates new form Data */
    public Data() {
        initComponents();
    }

    public synchronized void newRun(final long runTime, final long iteration, String parameters, final double error) {
        allValues.add(new HistoryPoint(runTime, iteration, error, parameters));
        update();
    }

    public synchronized void newSmallestError(final long runTime, final long iteration, String parameters, final double smallestError) {
        bestValues.add(new HistoryPoint(runTime, iteration, smallestError, parameters));
        update();
    }

    private synchronized void update() {
        if (isVisible()) {
            if (allValues.size() > 2000) {
                dataTextArea.setText("Too many values... only copy to clipboard is available.");
            }
            String text = generateText();
            dataTextArea.setText(text);
        }
    }

    private String generateText() {
        String text = "";
        text += "best = [" + "\n";
        for (HistoryPoint p : bestValues) {
            text += p.getRunTime() + ", " + p.getIteration() + ", " + (1 / p.getError()) + ";" + "\n";
        }
        text += "];" + "\n\n";
        text += "best_params = {" + "\n";
        for (HistoryPoint p : bestValues) {
            text += "'" + p.getParameters() + "'" + ";" + "\n";
        }
        text += "};" + "\n\n";
        text += "all = [" + "\n";
        for (HistoryPoint p : allValues) {
            text += p.getRunTime() + ", " + p.getIteration() + ", " + (1 / p.getError()) + ";" + "\n";
        }
        text += "];" + "\n\n";
        text += "all_params = {" + "\n";
        for (HistoryPoint p : allValues) {
            text += "'" + p.getParameters() + "'" + ";" + "\n";
        }
        text += "};" + "\n\n";
        return text;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        dataTextArea = new javax.swing.JTextArea();
        copyToClipboardButton = new javax.swing.JButton();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        dataTextArea.setColumns(20);
        dataTextArea.setEditable(false);
        dataTextArea.setRows(5);
        jScrollPane1.setViewportView(dataTextArea);

        copyToClipboardButton.setText("copy to clipboard");
        copyToClipboardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyToClipboardButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(copyToClipboardButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(copyToClipboardButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void copyToClipboardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyToClipboardButtonActionPerformed
        StringSelection stringSelection = new StringSelection(generateText());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, this);
    }//GEN-LAST:event_copyToClipboardButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        update();
    }//GEN-LAST:event_formComponentShown
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton copyToClipboardButton;
    private javax.swing.JTextArea dataTextArea;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void lostOwnership(Clipboard clipboard, Transferable contents) {
        // Do nothing
    }
}
